#include "print.h"
#include "cache.h"
#include "kernel.h"
#include "ecall.h"
#include "kmem.h"
#include "mmu.h"
#include "process.h"
#include "timer.h"
#include "plic.h"
#include "common.h"

void sinit(void)
{
    pr_info("M switch to S success.\n");

    rv_supervisor_trap_init();
#ifdef KERNEL_TEST
    RISCV_ECALL_0(0);
#endif
    os_kmem_init();
    pr_info("kernel memory allocator init success.\n");
#ifdef KERNEL_TEST
    kmem_test();
#endif
    os_mmu_init();
    pr_info("mmu init success.\n");
#if 0
    mmu_test();
    pr_test("after mmu_test.\n");
#endif
    os_proc_init();
#ifdef KERNEL_TEST
    proc_test();
#endif
    os_int_init();
    os_timer_start(1000);
    os_int_en(75, 1);

    os_start();

    while(1);
}

void init(void)
{
    pr_info("simpleos kernel is booting!\n");

#ifdef KERNEL_TEST
    printk_test();
#endif
    rv_cpu_init();
#ifdef KERNEL_TEST
	pr_test("before cache enable: ");
    cache_test();
#endif
    os_icache_enable();
    os_dcache_enable();
    pr_info("icache & dcache enable success.\n");
#ifdef KERNEL_TEST
	pr_test("after cache enable: ");
    cache_test();
#endif
    rv_plic_smode_access_en();
    rv_machine_trap_init();
    rv_machine_switchto_supervisor();
}